/*
*
*
* 事件驱动器
*
* */
define(function(){
    var scope,callback;
    var Event = function (callBack) {
        scope = this;
        callback = callBack || function () {};
        this.callback = callback;
        this.addEvent= function(dom,type,listener,options ){
            if( !typeof (dom) === 'object' )return;
            var name;
            if(dom.id!==''){
                var com = dom.id;
                if(eventFun[com]==undefined)eventFun[com]={};
            }
            eventFun[com].hasOwnProperty(listener.name)?name = listener.name + "A":name = listener.name;
            eventFun[com][name] = function(e){
                listener(e);
                console.log(listener.name);
                callback && callback();
            };
            eventFun[com][name].dom = dom;
            listener.dom = dom;
            dom.addEventListener(type, eventFun[com][name],options )
        };
        this.removeEvent= function(dom,type,listener,options ){
            var name;
            if( !typeof (dom) === 'object' ) return;
            if(!(typeof (listener) === 'function'? name = listener.name:typeof (listener) === 'string')? name = listener:false)return;
            if(dom.id!==''){
                dom.removeEventListener(type,eventFun[dom.id][name],options );
                delete eventFun[dom.id][name];
                var flag = name+"A";
                if(eventFun[dom.id].hasOwnProperty(flag)){
                    dom.removeEventListener(type,eventFun[dom.id][flag],options );
                    delete eventFun[dom.id][flag];
                }
            }
            
        };
        this.mouseMonitor= function (event){
            var returnObj;
            var flag = event.button;
            var wx = event.clientX;
            var wy = event.clientY;
            var wFx = button.winX;
            var wFy = button.winY;
            var offset = false;
            var eventFlag = wFx;
            if(eventFlag == null){
                button.winX = wx;
                button.winY = wy;
                return false;
            }else{
                if( wFx == wx &&  wFy == wy)offset = true;
                button.winX = null;
                button.winY = null;
            }
            return  returnObj = {
                flag: flag,
                enable: offset
            }
        };
    };
    
    // Object.assign(Event.prototype,{
    //     addEvent: function(dom,type,listener,options){
    //        if( !typeof (dom) === 'object' )return;
    //        eventFun[listener.name] = function(e){
    //            //e.preventDefault();
    //            //e.stopPropagation();
    //            listener(e);
    //           console.log(listener.dom.id);
    //            callback && callback();
    //        };
    //         eventFun[listener.name].dom = dom;
    //         listener.dom = dom;
    //        dom.addEventListener(type, eventFun[listener.name],options)
    //     },
    //
    //     removeEvent: function(dom,type,listener,options){
    //         var name;
    //         if( !typeof (dom) === 'object' ) debugger;return;
    //         if(!(typeof (listener) === 'function'? name = listener.name:typeof (listener) === 'string')? name = listener.name:false)return;
    //         dom.removeEventListener(type,eventFun[name],options);
    //         delete eventFun[listener.name];
    //     },
    //
    //     mouseMonitor: function (event){
    //         var returnObj;
    //         var flag = event.button;
    //         var wx = event.clientX;
    //         var wy = event.clientY;
    //         var wFx = button.winX;
    //         var wFy = button.winY;
    //         var offset = false;
    //         var eventFlag = wFx;
    //         if(eventFlag == null){
    //             button.winX = wx;
    //             button.winY = wy;
    //             return false;
    //         }else{
    //             if( wFx == wx &&  wFy == wy)offset = true;
    //             button.winX = null;
    //             button.winY = null;
    //         }
    //         return  returnObj = {
    //             flag: flag,
    //             enable: offset
    //         }
    //     },
    //
    //
    //
    // });
    var button = {
        winX:null,
        winY:null
    };          //用于判断鼠标指针位置
    var  eventFun = {};
    return Event;
});